<template>
  <van-popup
    v-model:show="showSubject"
    :position="position"
    closeable
    @closed="onCloseSubject"
    round
    :close-icon="iconClose"
  >
    <div class="popup-container">
      <div class="title-container">
        <div class="title-text">请选择三门考试科目</div>
      </div>
      <div class="main-subtitle-container">
        <div class="grid-container">
          <div
            :class="['grid-item', item.select ? 'active' : '']"
            v-for="(item, index) in typeTwoSubjects"
            :key="index"
            @click="onClickSubjectItem(item, index)"
          >
            {{ item.name }}
          </div>
        </div>
      </div>
      <div class="bottom-btn">
        <div class="right-btn" @click="onConfirmSubject">确 定</div>
      </div>
    </div>
  </van-popup>
</template>

<script setup>
import { reactive, ref, onMounted } from 'vue';
import { showToast } from 'vant';
import iconClose from '@/assets/images/closeBtn.png';

const props = defineProps(['subject']);

const emit = defineEmits(['onConfirmSubject', 'onCloseSubject']);

const showSubject = ref(true);

const typeTwoSubjects = ref([
  {
    name: '物理',
    id: 11,
    select: false,
  },
  {
    name: '历史',
    id: 12,
    select: false,
  },
  {
    name: '化学',
    id: 14,
    select: false,
  },
  {
    name: '生物',
    id: 13,
    select: false,
  },
  {
    name: '政治',
    id: 15,
    select: false,
  },
  {
    name: '地理',
    id: 16,
    select: false,
  },
]);

const position = ref('center');

const selectSubjects = ref([]);

const getSelectedData = () => {
  selectSubjects.value = props.subject.split('、');
  selectSubjects.value.forEach((item) => {
    const subject = typeTwoSubjects.value.find(
      (subject) => subject.name === item
    );
    if (subject) {
      subject.select = true;
    }
  });
};

getSelectedData();

const onClickSubjectItem = (item, index) => {
  const selectedItems = typeTwoSubjects.value.filter((item) => item.select);
  if (!item.select && selectedItems.length >= 3) {
    showToast('最多只能选择3个科目');
    return;
  }
  item.select = !item.select;
  selectSubjects.value = typeTwoSubjects.value.filter((item) => item.select);
};

const onConfirmSubject = () => {
  if (selectSubjects.value.length < 3) {
    showToast('请选择三门考试科目');
  } else {
    selectSubjects.value = typeTwoSubjects.value.filter((item) => item.select);
    emit('onConfirmSubject', 3, selectSubjects.value);
  }
};

const onCloseSubject = () => {
  emit('onCloseSubject');
};

onMounted(() => {});
</script>

<style lang="scss" scoped>
.popup-container {
  padding-bottom: 180px;
  .title-container {
    padding: 38px 0 20px 32px;
    display: flex;
    justify-content: space-between;

    .title-text {
      font-family: PingFang-SC, PingFang-SC;
      font-weight: bold;
      font-size: 34px;
      color: #222222;
      line-height: 28px;
      text-align: left;
      font-style: normal;
    }
  }
  .main-subtitle-container {
    margin-top: 24px;
    .grid-container {
      padding: 0 24px;
      display: grid;
      grid-template-columns: repeat(4, 1fr);
      gap: 14px;
      .grid-item {
        flex-shrink: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        border-radius: 8px;
        font-family: 'PingFang-SC, PingFang-SC';
        font-size: 30px;
        padding: 12px 36px;
        color: #333333;
        background: #f8f8f8;
        border: #ffffff 1px solid;
        &.active {
          color: $primary-color;
          border: $primary-color 1px solid;
        }
      }
    }
  }
  .bottom-btn {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 30px;
    background: #ffffff;
    box-shadow: 0px -2px 4px 0px rgba(188, 188, 188, 0.5);
    box-sizing: border-box;
    display: flex;
    flex-direction: row;
    justify-content: center;
    gap: 24px;
    z-index: 2;

    .left-btn,
    .right-btn {
      flex-shrink: 0;
      width: 98%;
      height: 72px;
      background: #f8f8f8;
      border-radius: 6px;
      font-family: PingFang-SC, PingFang-SC;
      font-weight: bold;
      font-size: 24px;
      color: #999999;
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: center;
    }

    .left-btn {
      background: #f8f8f8;
      border: 1px solid #f8f8f8;
      &.active {
        border: 1px solid $primary-color;
        color: $primary-color;
      }
    }
    .right-btn {
      color: #ffffff;
      background: $primary-color;
    }
  }
}
</style>
